<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Message_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
    /**
     * 表字段值
     */
    function message_fields($key = ''){
    	$data = array(
    		'fstatus'        => array(0=>'未读',1=>'已读'),
    		'message_type'   => array(),
    		'sented'         => array(0=>'不保存',1=>'保存到发件箱'),
    		'deltype'        => array(0=>'未删除',1=>'收件人删除',2=>'发件人删除',3=>'都删除')
    	);
    	
    	if($key && isset($data[$key])){
    		return $data[$key];
    	}else{
    		return $data;
    	}
    }
    
    function get_one($where = array(),$select='*'){
		$this->db->select($select);
		if(!empty($where)){
			$this->db->where($where);
		}
		$this->db->limit(1);
		$query = $this->db->get('rsh_message');
		return $query->row_array();
	}
    
    function get_list($where = array(),$select = '*'){
    	$this->db->select($select);
		if(!empty($where)){			
			$this->db->where($where);
		}				
		$query = $this->db->get('rsh_message');		
		return $query->result_array();
    }
    
    function update($data,$where){
    	if(!empty($where)){
    		$this->db->where($where);
    	}
		$this->db->update('rsh_message', $data);
		return $this->db->affected_rows();
    }
    
	/**
     * 发送消息
     */
    function send($parms = array()){
    	//$parms = array('发送用户','接收用户','标题','状态','类型','是否保存发件箱','是否删除','内容','添加时间','添加IP');
    	if(isset($parms['receive_user']) && $parms['receive_user'] >0){
    		$this->db->insert('rsh_message', $parms);
	    	$key = 'total_message_'.$parms['receive_user'];
	    	$this->mycache->delete($key);
	    	$key = 'not_read_count_'.$parms['receive_user'];
	    	$this->mycache->delete($key);
			return $this->db->insert_id();
    	}else{
    		return false;
    	}
    }
    
    /**
     * 删除
     */
    function del_message($ids,$user_id,$is_receive = 1){
    	if(!$ids) return false;
    	$p = $is_receive ? 1 : 2;
    	$sql = "update rsh_message set deltype=deltype|".$p." where ";
    	if(count($ids) == 1){
    		$sql .= " id = ". intval($ids[0]);
    	}else{
    		$sql .= " id in (". implode(',',$ids) . ")";
    	}
    	if($is_receive){
    		$sql .= " and receive_user='{$user_id}'";
    	}else{
    		$sql .= " and sent_user='{$user_id}'";
    	}
    	$query = $this->db->query($sql);
    	$key = 'total_message_'.$user_id;
    	$this->mycache->delete($key);
    	$key = 'not_read_count_'.$user_id;
    	$this->mycache->delete($key);
    	return $this->db->affected_rows();
    }
    
    /**
     * 更改"是否已读"状态
     */
    function read_message($ids,$user_id,$fstatus){
    	if(!$ids) return false;
    	$sql = "update rsh_message set fstatus='{$fstatus}' where receive_user='{$user_id}' and ";
    	if(count($ids) == 1){
    		$sql .= " id = ". intval($ids[0]);
    	}else{
    		$sql .= " id in (". implode(',',$ids) . ")";
    	}
    	$query = $this->db->query($sql);
    	$key = 'not_read_count_'.$user_id;
    	$this->mycache->delete($key);
    	return $this->db->affected_rows();
    }
    
    
    /**
     * 收件箱
     */
    function receive_box($receive_user,$page=0,$page_size=20){
    	//$limit = ($page-1)*$page_size;
    	$sql = "select p1.*,p2.username as sent_username 
    			from rsh_message as p1 left join rsh_user as p2 on p1.sent_user=p2.user_id 
    			where p1.receive_user='{$receive_user}' and p1.deltype&1=0 order by id desc limit {$page},{$page_size}";
    	$query = $this->db->query($sql);
    	$result['list'] = $query->result_array();
    	
    	$sql = "select count(id) as count from rsh_message where receive_user='{$receive_user}' and deltype&1=0;";
		$query = $this->db->query($sql);
		$result['count'] = $query->row()->count;
		return $result;
    }
    
    /**
     * 发件箱
     */
    function send_box($sent_user,$page=0,$page_size=20){
    	$sql = "select p1.*,p2.username as receive_username 
    			from rsh_message as p1 left join rsh_user as p2 on p1.receive_user=p2.user_id 
    			where p1.sent_user='{$sent_user}' and p1.deltype&2=0 order by id desc limit {$page},{$page_size}";
    	$query = $this->db->query($sql);
    	$result['list'] = $query->result_array();
    	
    	$sql = "select count(id) as count from rsh_message where sent_user='{$sent_user}' and deltype&2=0;";
		$query = $this->db->query($sql);
		$result['count'] = $query->row()->count;
		return $result;
    }
    
    
    
    /**
     * 
     * 站内信管理列表
     * @param $where
     * @param $select
     */
	function get_message_backup_list($data){
    	//封装查询内容
		$select = "id,sent_user,receive_user,title,fstatus,message_type,deltype,content,add_time";
		
		//封装条件
		$_where = " where deltype=0 ";
		
		//排序
		$order  = " order by id desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_message_backup";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['message_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_message_backup";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['message_count'] = $query->row()->count;
		return $data;
    }
    
    
    /**
     * 
     * 记录站内信
     */
    function add_message_backup($data){
    	$this->db->insert('rsh_message_backup', $data);
		return $this->db->insert_id();
    }
    
    
    
}